<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="登录账号" prop="account">
				<el-input v-model="form.account" placeholder="用于登录系统" clearable>
          <template #prepend>om:</template>
        </el-input>
			</el-form-item>
			<el-form-item label="姓名" prop="realname">
				<el-input v-model="form.realname" placeholder="请输入完整的真实姓名" clearable></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" placeholder="请输入备注" clearable></el-input>
			</el-form-item>
			<template v-if="mode=='add'">
				<el-form-item label="登录密码" prop="password">
					默认密码：123456
				</el-form-item>
			</template>
<!--			<el-form-item label="所属部门" prop="deptIdList">-->
<!--				<el-cascader v-model="form.deptIdList" :options="groups" :props="groupsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>-->
<!--			</el-form-item>-->
      <el-form-item label="所属部门" prop="dept_id">
				<el-cascader v-model="form.dept_id" :options="groups" :props="groupsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
			</el-form-item>
      <el-form-item label="职位" prop="roleIdList">
        <el-cascader v-model="form.roleIdList" :options="groupsRole" :props="groupsRoleProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
      </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import api from '@/api/index.js'
import tool from '@/utils/tool.js'
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增人员',
					edit: '编辑人员',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:0,
					account: "",
					avatar: "",
					nickname: "",
					phone: "",
					realname: "",
					remark: "",
          dept_id: "",
          deptIdList: [],
          roleIdList: [],
					status: 1
				},
				//验证规则
				rules: {
					account: [
						{ required: true, message: '请输入登录账号', trigger: 'blur' },
						{ min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
					],
				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					label: "name",
					// multiple: true,
					checkStrictly: true
				},
        groupsRole: [],
        groupsRoleProps: {
					value: "id",
					label: "name",
					multiple: true,
					checkStrictly: true
				}
			}
		},
		mounted() {
			this.getGroup()
			this.getRoleGroup()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//加载树数据
			async getGroup(){
				var res = await api.system.dept.list();
				this.groups = tool.deepTree(res.data,'parent_id')
			},
      //加载树数据
      async getRoleGroup(){
        var res = await api.system.position.list();
        this.groupsRole = res.data;
      },
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						let roleIds = [];
            if(this.form.roleIdList && this.form.roleIdList.length > 0){
              this.form.roleIdList.map(item => {
                if (tool.isArray(item)) {
                  roleIds.push(item[item.length - 1])
                }
                else{
                  roleIds.push(item)
                }
              })
            }
						let submitData = {};
						Object.assign(submitData, this.form);
						submitData.roleIdList = roleIds;
            if (tool.isArray(submitData.dept_id)) {
              submitData.dept_id = submitData.dept_id[submitData.dept_id.length - 1]
            }
						let methodName;
						if (this.form.id > 0) {
							methodName = 'update'
						}
						else {
							methodName = 'add'
						}
						let res = await api.system.orgMember[methodName](submitData);
						this.isSaveing = false;
						if(res.code === 0){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			async setData(data){
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				// console.log(data)
				// Object.assign(this.form, data)
				this.isSaveing = true;
				let res = await api.system.orgMember.info({id: data.id})
				console.log(res)
				this.form = res.data
				this.isSaveing = false;
			}
		}
	}
</script>

<style>
</style>
